<!DOCTYPE html>
<html  lang="zh">
<head>
    <!-- hexo-inject:begin --><!-- hexo-inject:end --><meta charset="utf-8" />

<meta name="generator" content="Hexo 3.9.0" />

<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />

<title>shader学习4-纹理凹凸映射 - 游戏人生</title>


    <meta name="description" content="shade学习">
<meta name="keywords" content="学习">
<meta property="og:type" content="article">
<meta property="og:title" content="shader学习4-纹理凹凸映射">
<meta property="og:url" content="http://cloundgame.gitee.io/mystudy/2020/02/21/shader学习4-纹理凹凸映射/index.html">
<meta property="og:site_name" content="游戏人生">
<meta property="og:description" content="shade学习">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://cloundgame.gitee.io/mystudy/images/og_image.png">
<meta property="og:updated_time" content="2020-02-24T10:04:37.616Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="shader学习4-纹理凹凸映射">
<meta name="twitter:description" content="shade学习">
<meta name="twitter:image" content="http://cloundgame.gitee.io/mystudy/images/og_image.png">







<link rel="icon" href="/mystudy/images/favicon.svg">


<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.7.2/css/bulma.css">
<link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/font-awesome/5.9.0/css/all.min.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Ubuntu:400,600|Source+Code+Pro">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@9.12.0/styles/atom-one-dark.css">


    
    
    
    <style>body>.footer,body>.navbar,body>.section{opacity:0}</style>
    

    
    
    
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/lightgallery@1.6.8/dist/css/lightgallery.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/css/justifiedGallery.min.css">
    

    
    

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/outdatedbrowser@1.1.5/outdatedbrowser/outdatedbrowser.min.css">


    
    
    
    

<link rel="stylesheet" href="/mystudy/css/back-to-top.css">


    
    

    
    
    
    

    
    
<link rel="stylesheet" href="/mystudy/css/progressbar.css">
<script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>

    
    
    
        <script async="" src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    

    


<link rel="stylesheet" href="/mystudy/css/style.css"><!-- hexo-inject:begin --><!-- hexo-inject:end -->
</head>
<body class="is-3-column">
    <!-- hexo-inject:begin --><!-- hexo-inject:end --><nav class="navbar navbar-main">
    <div class="container">
        <div class="navbar-brand is-flex-center">
            <a class="navbar-item navbar-logo" href="/mystudy/">
            
                <img src="/mystudy/images/logo.svg" alt="shader学习4-纹理凹凸映射" height="28">
            
            </a>
        </div>
        <div class="navbar-menu">
            
            <div class="navbar-start">
                
                <a class="navbar-item"
                href="/mystudy/">主页</a>
                
                <a class="navbar-item"
                href="/mystudy/archives">日志</a>
                
                <a class="navbar-item"
                href="/mystudy/categories">分类</a>
                
                <a class="navbar-item"
                href="/mystudy/">关于</a>
                
            </div>
            
            <div class="navbar-end">
                
                    
                    
                    <a class="navbar-item" target="_blank" title="Download on GitHub" href="https://github.com/koliy/koliy.github.io">
                        
                        <i class="fab fa-github"></i>
                        
                    </a>
                    
                
                
                <a class="navbar-item is-hidden-tablet catalogue" title="目录" href="javascript:;">
                    <i class="fas fa-list-ul"></i>
                </a>
                
                
                <a class="navbar-item search" title="搜索" href="javascript:;">
                    <i class="fas fa-search"></i>
                </a>
                
            </div>
        </div>
    </div>
</nav>
    
    <section class="section">
        <div class="container">
            <div class="columns">
                <div class="column is-8-tablet is-8-desktop is-6-widescreen has-order-2 column-main"><div class="card">
    
    <div class="card-content article ">
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2020-02-21T10:52:12.000Z">2020-02-21</time>
                
                <div class="level-item">
                <a class="has-link-grey -link" href="/mystudy/categories/shade学习/">shade学习</a>
                </div>
                
                
                <span class="level-item has-text-grey">
                    
                    
                    15 分钟 读完 (大约 2296 个字)
                </span>
                
                
                <span class="level-item has-text-grey" id="busuanzi_container_page_pv">
                    <i class="far fa-eye"></i>
                    <span id="busuanzi_value_page_pv">0</span>次访问
                </span>
                
            </div>
        </div>
        
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                shader学习4-纹理凹凸映射
            
        </h1>
        <div class="content">
            <h2 id="纹理凹凸映射"><a href="#纹理凹凸映射" class="headerlink" title="纹理凹凸映射"></a><strong>纹理凹凸映射</strong></h2><p>凹凸映射的目的是使用一张纹理来修改模型表面的法线，以便为模型提供更多的细节,这种方法不会改变模型的顶点位置，只是让模型看起来像凹凸不平的。<br><strong>有2种主要方法用于凹凸映射:1,高度纹理,2,法线纹理(normal map)</strong></p>
<a id="more"></a>
<h4 id="高度纹理"><a href="#高度纹理" class="headerlink" title="高度纹理"></a><strong>高度纹理</strong></h4><p>在Unity中对纹理类型设置Normal map，并勾选Create from Grayscale，则代表使用高度纹理。</p>
<h4 id="法线纹理"><a href="#法线纹理" class="headerlink" title="法线纹理"></a><strong>法线纹理</strong></h4><p><strong>法线纹理中存储的是物体表面的法线方向</strong><br><strong>由于法线的方向的分量范围为[-1,1],而像素的分量范围为[0,1],因此法线方向存储到纹理中，换算成像素的公式为:</strong><br>$$<br>pixel = \frac{normal +1}{2} = 0.5*normal +0.5<br>$$</p>
<p><strong>因此，在Shader中，我们通过tex2D采集到纹素后，需要根据公式反映射出normal法线方向:</strong><br>$$<br>normal = pixel *2 -1<br>$$</p>
<p><strong>由于方向是相对坐标空间来说的，因此需要确定法线纹理中存储的法线方向在哪个坐标空间</strong><br><strong>在实际制作中，采用另一种坐标空间，既模型顶点的切线空间来存储法线</strong></p>
<p><strong>因此：法线纹理中存储的法线是切线空间下的方向。</strong><br><strong>我们在计算光照模型中需要统一各个方向矢量所在的坐标空间，使用法线纹理，我们通常有两种选择:</strong></p>
<ul>
<li><strong>在切线空间下进行光照计算,此时我们需要把光源方向，视角方向变换到切线空间</strong></li>
<li><strong>在世界空间下进行光照计算，此时需要把采样到的法线方向变换到世界空间下，再和世界空间下的光源方向，视角方向进行计算</strong></li>
</ul>
<h6 id="1-在切线空间下计算"><a href="#1-在切线空间下计算" class="headerlink" title="1. 在切线空间下计算"></a><strong>1. 在切线空间下计算</strong></h6><p>基本思路: 在片元着色器中通过纹理采样得到切线空间下的法线，然后再与切线空间下的视角方向，光源方向进行计算，得到最终光照结果.</p>
<figure class="highlight javascript hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br></pre></td><td class="code"><pre><span class="line">Shader<span class="hljs-string">"MyTest/Test2/blinnphone-normaltex"</span>&#123;</span><br><span class="line"></span><br><span class="line">	Properties&#123;</span><br><span class="line">		_Color(<span class="hljs-string">"Color"</span>,Color)=(<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-number">1</span>)</span><br><span class="line">		_MainTex(<span class="hljs-string">"Main Texture"</span>,<span class="hljs-number">2</span>D)=<span class="hljs-string">"white"</span>&#123;&#125;</span><br><span class="line">		_NormalTex(<span class="hljs-string">"Normal Texture"</span>,<span class="hljs-number">2</span>D)=<span class="hljs-string">"bump"</span>&#123;&#125;</span><br><span class="line">		_BumpScale(<span class="hljs-string">"Bump Scale"</span>,float)=<span class="hljs-number">1.0</span></span><br><span class="line">		_Specular(<span class="hljs-string">"Specular Color"</span>,Color)=(<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-number">1</span>)</span><br><span class="line">		_Glass(<span class="hljs-string">"Glass int"</span>,Range(<span class="hljs-number">8.0</span>,<span class="hljs-number">256</span>))=<span class="hljs-number">20</span></span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	SubShader&#123;</span><br><span class="line">		Tags&#123;<span class="hljs-string">"RenderType"</span>=<span class="hljs-string">"Opaque"</span>&#125;</span><br><span class="line"></span><br><span class="line">		Pass&#123;</span><br><span class="line">			Tags&#123;<span class="hljs-string">"LightMode"</span>=<span class="hljs-string">"Forwardbase"</span>&#125;</span><br><span class="line"></span><br><span class="line">			CGPROGRAM</span><br><span class="line">			#include "UnityCG.cginc"</span><br><span class="line">			#include "Lighting.cginc"</span><br><span class="line"></span><br><span class="line">			fixed4 _Color;</span><br><span class="line">			sampler2D _MainTex;</span><br><span class="line">			float4 _MainTex_ST;</span><br><span class="line">			sampler2D _NormalTex;</span><br><span class="line">			float4 _NormalTex_ST;</span><br><span class="line"></span><br><span class="line">			fixed4 _Specular;</span><br><span class="line">			float _Glass;</span><br><span class="line">			float _BumpScale;</span><br><span class="line"></span><br><span class="line">			struct v2i&#123;</span><br><span class="line">				float4 pos:POSITION;</span><br><span class="line">				float4 tangent:TANGENT;</span><br><span class="line">				float3 normal:NORMAL;</span><br><span class="line"></span><br><span class="line">				float4 maintex:TEXCOORD0;</span><br><span class="line">			&#125;;</span><br><span class="line"></span><br><span class="line">			struct v2f&#123;</span><br><span class="line">				float4 pos:SV_POSITION;</span><br><span class="line">				float4 uv:TEXCOORD0;</span><br><span class="line">				float3 tangentlight:TEXCOORD1;</span><br><span class="line">				float3 tangentview:TEXCOORD2;</span><br><span class="line"></span><br><span class="line">			&#125;;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">			#pragma vertex vert</span><br><span class="line">			#pragma fragment frag</span><br><span class="line"></span><br><span class="line">			v2f vert(v2i v)&#123;</span><br><span class="line">				v2f o;</span><br><span class="line">				o.pos = UnityObjectToClipPos(v.pos);</span><br><span class="line">				<span class="hljs-comment">//获取最终纹理坐标</span></span><br><span class="line">				o.uv.xy = TRANSFORM_TEX(v.maintex,_MainTex);</span><br><span class="line">				o.uv.zw = TRANSFORM_TEX(v.maintex,_NormalTex);</span><br><span class="line"></span><br><span class="line">				<span class="hljs-comment">//获取切线空间下的矩阵rotation</span></span><br><span class="line">				TANGENT_SPACE_ROTATION;</span><br><span class="line"></span><br><span class="line">				<span class="hljs-comment">//模型空间下的光照方向转换到切线空间下的光照方向</span></span><br><span class="line">				o.tangentlight= mul(rotation,normalize(ObjSpaceLightDir(v.pos))).xyz;</span><br><span class="line">				<span class="hljs-comment">//模型空间下的视角方向转换到切线空间下的视角方向</span></span><br><span class="line">				o.tangentview = mul(rotation,normalize(ObjSpaceViewDir(v.pos))).xyz;</span><br><span class="line"></span><br><span class="line">				<span class="hljs-keyword">return</span> o;</span><br><span class="line">			&#125;</span><br><span class="line"></span><br><span class="line">			fixed4 frag(v2f o):SV_Target&#123;</span><br><span class="line">			</span><br><span class="line">				fixed3 albedo = tex2D(_MainTex,o.uv.xy).rgb *_Color.rgb;</span><br><span class="line">				fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;</span><br><span class="line"></span><br><span class="line">				float3 lightdir = normalize(o.tangentlight);</span><br><span class="line">				float3 viewdir = normalize(o.tangentview);</span><br><span class="line"></span><br><span class="line">				<span class="hljs-comment">//获取法线纹理的纹素</span></span><br><span class="line">				fixed4 packnormal = tex2D(_NormalTex,o.uv.zw);</span><br><span class="line">				<span class="hljs-comment">//通过反映射公式，求得法线xy轴的方向:normal = pixel *2 -1</span></span><br><span class="line">				fixed3 tangentnormal = UnpackNormal(packnormal);</span><br><span class="line">				<span class="hljs-comment">//通过_BumpScale控制凹凸程度</span></span><br><span class="line">				tangentnormal.xy *= _BumpScale;</span><br><span class="line"></span><br><span class="line">				<span class="hljs-comment">//计算切线空间下的漫反射</span></span><br><span class="line">				fixed3 diffuse = _LightColor0.rgb * albedo.rgb * (<span class="hljs-number">0.5</span>*dot(tangentnormal,lightdir)+<span class="hljs-number">0.5</span>);</span><br><span class="line"></span><br><span class="line">				<span class="hljs-comment">//blinnphone</span></span><br><span class="line">				fixed3 blinnphone = normalize(lightdir+viewdir);</span><br><span class="line">				fixed3 specular = _LightColor0.rgb * _Specular *(pow(saturate(dot(tangentnormal,blinnphone)),_Glass));</span><br><span class="line"></span><br><span class="line">				fixed3 color = ambient + diffuse+specular;</span><br><span class="line">				<span class="hljs-keyword">return</span> fixed4(color,<span class="hljs-number">1</span>);</span><br><span class="line">			&#125;</span><br><span class="line"></span><br><span class="line">			ENDCG</span><br><span class="line"></span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	Fallback <span class="hljs-string">"Specular"</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li><strong>1. 在顶点着色器中把视角方向和光照方向从模型空间变换到切线空间</strong></li>
</ul>
<p><strong>TANGENT_SPACE_ROTATION：</strong> 宏定义，返回切线空间下是矩阵rotation<br>矩阵一般为3x3大小，对应x，y，z轴。对于模型的每个顶点，它都有一个属于自己的切线空间，这个切线空间的原点就是该顶点本身，而z轴就是顶点的法线方向(normal),x轴就是顶点的切线方向，y轴可以由法线和切线的叉积获得，因为3轴互相垂直,y轴也叫副切线。<br>因此，矩阵很容易获得: 按切线(x轴),副切线(y轴)，法线(z轴)的顺序按行排列获得。</p>
<p>v.tangent.w ：保留了切线方向</p>
<figure class="highlight javascript hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-comment">// Declares 3x3 matrix 'rotation', filled with tangent space basis</span></span><br><span class="line">#define TANGENT_SPACE_ROTATION \</span><br><span class="line">    float3 binormal = cross( normalize(v.normal), normalize(v.tangent.xyz) ) * v.tangent.w; \</span><br><span class="line">    float3x3 rotation = float3x3( v.tangent.xyz, binormal, v.normal )</span><br></pre></td></tr></table></figure>

<figure class="highlight javascript hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-comment">//获取切线空间下的矩阵rotation</span></span><br><span class="line">TANGENT_SPACE_ROTATION;</span><br><span class="line"></span><br><span class="line"><span class="hljs-comment">//模型空间下的光照方向转换到切线空间下的光照方向</span></span><br><span class="line">o.tangentlight= mul(rotation,normalize(ObjSpaceLightDir(v.pos))).xyz;</span><br><span class="line"><span class="hljs-comment">//模型空间下的视角方向转换到切线空间下的视角方向</span></span><br><span class="line">o.tangentview = mul(rotation,normalize(ObjSpaceViewDir(v.pos))).xyz;</span><br></pre></td></tr></table></figure>

<ul>
<li><strong>2.在片元着色器中，通过采集到的法线，和切线空间下的视角，光源方向进行计算</strong></li>
</ul>
<figure class="highlight javascript hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-comment">//获取法线纹理的纹素</span></span><br><span class="line">fixed4 packnormal = tex2D(_NormalTex,o.uv.zw);</span><br><span class="line"><span class="hljs-comment">//通过反映射公式，求得法线xy轴的方向:normal = pixel *2 -1</span></span><br><span class="line">fixed3 tangentnormal = UnpackNormal(packnormal);</span><br><span class="line"><span class="hljs-comment">//通过_BumpScale控制凹凸程度</span></span><br><span class="line">tangentnormal.xy *= _BumpScale;</span><br><span class="line"></span><br><span class="line"><span class="hljs-comment">//计算切线空间下的漫反射</span></span><br><span class="line">fixed3 diffuse = _LightColor0.rgb * albedo.rgb * (<span class="hljs-number">0.5</span>*dot(tangentnormal,lightdir)+<span class="hljs-number">0.5</span>);</span><br></pre></td></tr></table></figure>

<p><strong>UnpackNormal:</strong> 输入纹素，根据反映射公式 $normal = pixel *2 -1$，返回纹理中的法线方向xyz值，因此不需要特意再计算z的值。</p>
<p><strong>法线纹理类型标记为Normal Map，可以让unity根据不同平台对纹理进行压缩(DXT5nm格式),减少法线纹理占用的内存空间.此函数可以根据不同的压缩格式对法线纹理进行正确的采样</strong></p>
<figure class="highlight javascript hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-comment">// Unpack normal as DXT5nm (1, y, 1, x) or BC5 (x, y, 0, 1)</span></span><br><span class="line"><span class="hljs-comment">// Note neutral texture like "bump" is (0, 0, 1, 1) to work with both plain RGB normal and DXT5nm/BC5</span></span><br><span class="line">fixed3 UnpackNormalmapRGorAG(fixed4 packednormal)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="hljs-comment">// This do the trick</span></span><br><span class="line">   packednormal.x *= packednormal.w;</span><br><span class="line"></span><br><span class="line">    fixed3 normal;</span><br><span class="line">    normal.xy = packednormal.xy * <span class="hljs-number">2</span> - <span class="hljs-number">1</span>;</span><br><span class="line">    normal.z = sqrt(<span class="hljs-number">1</span> - saturate(dot(normal.xy, normal.xy)));</span><br><span class="line">    <span class="hljs-keyword">return</span> normal;</span><br><span class="line">&#125;</span><br><span class="line">inline fixed3 UnpackNormal(fixed4 packednormal)</span><br><span class="line">&#123;</span><br><span class="line">#if defined(UNITY_NO_DXT5nm)</span><br><span class="line">    <span class="hljs-keyword">return</span> packednormal.xyz * <span class="hljs-number">2</span> - <span class="hljs-number">1</span>;</span><br><span class="line">#else</span><br><span class="line">    <span class="hljs-keyword">return</span> UnpackNormalmapRGorAG(packednormal);</span><br><span class="line">#endif</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h6 id="2-在世界空间下计算"><a href="#2-在世界空间下计算" class="headerlink" title="2. 在世界空间下计算"></a><strong>2. 在世界空间下计算</strong></h6><p>基本思路:</p>
<ul>
<li><p><strong>在顶点着色器中计算从切线空间到世界空间的变换矩阵，然后传递给片元着色器.</strong></p>
</li>
<li><p><strong>在片元着色器中把法线纹理中的法线方向从切线空间变换到世界空间下</strong></p>
</li>
</ul>
<p><strong>尽管在世界空间下计算需要更多的计算，但在需要使用CubeMap进行环境映射等情况下时，我们就需要使用这种方法</strong></p>
<figure class="highlight javascript hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-comment">// Upgrade <span class="hljs-doctag">NOTE:</span> replaced '_Object2World' with 'unity_ObjectToWorld'</span></span><br><span class="line"></span><br><span class="line">Shader <span class="hljs-string">"MyTest/Test2/blinnphone-normaltexworld"</span>&#123;</span><br><span class="line"></span><br><span class="line">	Properties&#123;</span><br><span class="line">		_Color(<span class="hljs-string">"Color"</span>,Color)=(<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-number">1</span>)</span><br><span class="line">		_MainTex(<span class="hljs-string">"Main Tex"</span>,<span class="hljs-number">2</span>D)=<span class="hljs-string">"white"</span>&#123;&#125;</span><br><span class="line">		_NormalTex(<span class="hljs-string">"Normal Tex"</span>,<span class="hljs-number">2</span>D)=<span class="hljs-string">"bump"</span>&#123;&#125;</span><br><span class="line">		_BumpScale(<span class="hljs-string">"Bump Scale"</span>,float)=<span class="hljs-number">1.0</span></span><br><span class="line">		_Specular(<span class="hljs-string">"Specular"</span>,Color)=(<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-number">1</span>)</span><br><span class="line">		_Glass(<span class="hljs-string">"Glass"</span>,Range(<span class="hljs-number">0.8</span>,<span class="hljs-number">256</span>))=<span class="hljs-number">20</span></span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	SubShader&#123;</span><br><span class="line">		Tags&#123;<span class="hljs-string">"RenderType"</span>=<span class="hljs-string">"Opaque"</span>&#125;</span><br><span class="line">		Pass&#123;</span><br><span class="line">			Tags&#123;<span class="hljs-string">"LightMode"</span>=<span class="hljs-string">"Forwardbase"</span>&#125;</span><br><span class="line"></span><br><span class="line">			CGPROGRAM</span><br><span class="line"></span><br><span class="line">			#include "UnityCG.cginc"</span><br><span class="line">			#include "Lighting.cginc"</span><br><span class="line"></span><br><span class="line">			fixed4 _Color;</span><br><span class="line">			fixed4 _Specular;</span><br><span class="line">			float _Glass;</span><br><span class="line">			sampler2D _MainTex;</span><br><span class="line">			float4 _MainTex_ST;</span><br><span class="line">			sampler2D _NormalTex;</span><br><span class="line">			float4 _NormalTex_ST;</span><br><span class="line">			float _BumpScale;</span><br><span class="line"></span><br><span class="line">			struct a2v&#123;</span><br><span class="line">				float4 vertex:POSITION;</span><br><span class="line">				float3 normal:NORMAL;</span><br><span class="line">				float4 tangent:TANGENT;</span><br><span class="line">				float4 textcoord:TEXCOORD0;	</span><br><span class="line">			&#125;;</span><br><span class="line"></span><br><span class="line">			struct v2f&#123;</span><br><span class="line">				float4 pos:SV_POSITION;</span><br><span class="line">				float4 uv:TEXCOORD0;</span><br><span class="line">				float4 tw0:TEXCOORD1;</span><br><span class="line">				float4 tw1:TEXCOORD2;</span><br><span class="line">				float4 tw2:TEXCOORD3;</span><br><span class="line">			&#125;;</span><br><span class="line"></span><br><span class="line">			#pragma vertex vert</span><br><span class="line">			#pragma fragment frag</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">			</span><br><span class="line">			v2f vert(a2v v)</span><br><span class="line">			&#123;</span><br><span class="line">				v2f o;</span><br><span class="line">				o.pos = UnityObjectToClipPos(v.vertex);</span><br><span class="line">				o.uv.xy = TRANSFORM_TEX(v.textcoord,_MainTex);</span><br><span class="line">				o.uv.zw = TRANSFORM_TEX(v.textcoord,_NormalTex);</span><br><span class="line"></span><br><span class="line">				float3 worldpos = mul(unity_ObjectToWorld,v.vertex).xyz;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">				<span class="hljs-comment">//每个顶点的切线空间矩阵转换到世界空间下</span></span><br><span class="line">				float3 tangentworldX = UnityObjectToWorldDir(v.tangent.xyz);</span><br><span class="line">				float3 normalworldZ = UnityObjectToWorldNormal(v.normal);</span><br><span class="line">				<span class="hljs-comment">//获取y轴</span></span><br><span class="line">				float3 binormalworldY = cross(normalworldZ,tangentworldX) * v.tangent.w;</span><br><span class="line">				<span class="hljs-comment">//矩阵按行排列传递给片元着色器</span></span><br><span class="line">				o.tw0 = float4(tangentworldX.x,binormalworldY.x,normalworldZ.x,worldpos.x);</span><br><span class="line">				o.tw1 = float4(tangentworldX.y,binormalworldY.y,normalworldZ.y,worldpos.y);</span><br><span class="line">				o.tw2 = float4(tangentworldX.z,binormalworldY.z,normalworldZ.z,worldpos.z);</span><br><span class="line"></span><br><span class="line">				<span class="hljs-keyword">return</span> o;</span><br><span class="line">			&#125;</span><br><span class="line">			</span><br><span class="line">			fixed4 frag(v2f v):SV_Target&#123;</span><br><span class="line">				float3 worldpos = float3(v.tw0.w,v.tw1.w,v.tw2.w);</span><br><span class="line">				fixed3 albedo = tex2D(_MainTex,v.uv.xy).rgb *_Color.rgb;</span><br><span class="line">				fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;</span><br><span class="line"></span><br><span class="line">				float3 worldlight = normalize( UnityWorldSpaceLightDir(worldpos));</span><br><span class="line">				float3 worldview = normalize(UnityWorldSpaceViewDir(worldpos));</span><br><span class="line">				<span class="hljs-comment">//获取切线纹理中的切线空间下的法线方向</span></span><br><span class="line">				fixed4 packnormal = tex2D(_NormalTex,v.uv.zw);</span><br><span class="line">				fixed3 tangentnormal = UnpackNormal(packnormal);</span><br><span class="line">				tangentnormal.xy *= _BumpScale;</span><br><span class="line">				<span class="hljs-comment">//和世界空间下的切线矩阵相乘，转换为世界空间下的切线</span></span><br><span class="line">				float3 worldtangent = float3(v.tw0.x,v.tw1.x,v.tw2.x);</span><br><span class="line">				float3 worldbinomal = float3(v.tw0.y,v.tw1.y,v.tw2.y);</span><br><span class="line">				float3 worldnormal = float3(v.tw0.z,v.tw1.z,v.tw2.z);</span><br><span class="line"></span><br><span class="line">				float3x3 rotation = float3x3(worldtangent, worldbinomal, worldnormal );</span><br><span class="line">				fixed3 worldtangentnormal = normalize(mul(rotation,normalize(tangentnormal)).xyz);</span><br><span class="line"></span><br><span class="line">				fixed3 diffuse = _LightColor0.rgb * albedo *(<span class="hljs-number">0.5</span>*dot(worldtangentnormal,worldlight)+<span class="hljs-number">0.5</span>);</span><br><span class="line"></span><br><span class="line">				fixed3 blinnphone = normalize(worldview + worldlight);</span><br><span class="line">				fixed3 specualr = _LightColor0.rgb * _Specular * pow(max(<span class="hljs-number">0</span>,dot(worldtangentnormal,blinnphone)),_Glass);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">				fixed3 color = ambient+diffuse+specualr;</span><br><span class="line">				<span class="hljs-keyword">return</span> fixed4(color,<span class="hljs-number">1</span>);</span><br><span class="line">			&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">			ENDCG</span><br><span class="line"></span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	Fallback <span class="hljs-string">"Specular"</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>先把切线空间下的x,y,z轴转换到世界空间下:</li>
</ul>
<figure class="highlight javascript hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-comment">//每个顶点的切线空间矩阵转换到世界空间下</span></span><br><span class="line">float3 tangentworldX = UnityObjectToWorldDir(v.tangent.xyz);</span><br><span class="line">float3 normalworldZ = UnityObjectToWorldNormal(v.normal);</span><br><span class="line"><span class="hljs-comment">//获取y轴</span></span><br><span class="line">float3 binormalworldY = cross(normalworldZ,tangentworldX) * v.tangent.w;</span><br></pre></td></tr></table></figure>

<ul>
<li>然后在片元着色器中，按行排列为一个世界空间下的切线矩阵：</li>
</ul>
<figure class="highlight javascript hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-comment">//和世界空间下的切线矩阵相乘，转换为世界空间下的切线</span></span><br><span class="line">float3 worldtangent = float3(v.tw0.x,v.tw1.x,v.tw2.x);</span><br><span class="line">float3 worldbinomal = float3(v.tw0.y,v.tw1.y,v.tw2.y);</span><br><span class="line">float3 worldnormal = float3(v.tw0.z,v.tw1.z,v.tw2.z);</span><br><span class="line"></span><br><span class="line">float3x3 rotation = float3x3(worldtangent, worldbinomal, worldnormal );</span><br></pre></td></tr></table></figure>

<ul>
<li>接着切线空间下的法线方向，右乘世界空间下的切线矩阵，得到世界空间下的法线方向:</li>
</ul>
<figure class="highlight javascript hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">float3x3 rotation = float3x3(worldtangent, worldbinomal, worldnormal );</span><br><span class="line">fixed3 worldtangentnormal = normalize(mul(rotation,normalize(tangentnormal)).xyz);</span><br></pre></td></tr></table></figure>


        </div>
        
        <div class="level is-size-7 is-uppercase">
            <div class="level-start">
                <div class="level-item">
                    <span class="is-size-6 has-text-grey has-mr-7">#</span>
                    <a class="has-link-grey -link" href="/mystudy/tags/学习/">学习</a>
                </div>
            </div>
        </div>
        
        

        
        <div class="social-share"></div>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js@1.0.16/dist/css/share.min.css">
<script src="https://cdn.jsdelivr.net/npm/social-share.js@1.0.16/dist/js/social-share.min.js"></script>
        
    </div>
</div>





<div class="card card-transparent">
    <div class="level post-navigation is-flex-wrap is-mobile">
        
        <div class="level-start">
            <a class="level level-item has-link-grey  article-nav-prev" href="/mystudy/2020/02/24/shader学习5-渐变纹理/">
                <i class="level-item fas fa-chevron-left"></i>
                <span class="level-item">shader学习5-渐变纹理</span>
            </a>
        </div>
        
        
        <div class="level-end">
            <a class="level level-item has-link-grey  article-nav-next" href="/mystudy/2020/02/20/shader学习3-基础纹理/">
                <span class="level-item">shader学习3-基础纹理</span>
                <i class="level-item fas fa-chevron-right"></i>
            </a>
        </div>
        
    </div>
</div>


</div>
                




<div class="column is-4-tablet is-4-desktop is-3-widescreen  has-order-1 column-left ">
    
        
<div class="card widget">
    <div class="card-content">
        <nav class="level">
            <div class="level-item has-text-centered" style="flex-shrink: 1">
                <div>
                    
                        <img class="image is-128x128 has-mb-6" src="/mystudy/images/avatar2.jpg" alt="满头大汉老黄牛">
                    
                    
                    <p class="is-size-4 is-block">
                        满头大汉老黄牛
                    </p>
                    
                    
                    <p class="is-size-6 is-block">
                        游戏
                    </p>
                    
                    
                    <p class="is-size-6 is-flex is-flex-center has-text-grey">
                        <i class="fas fa-map-marker-alt has-mr-7"></i>
                        <span>火星</span>
                    </p>
                    
                </div>
            </div>
        </nav>
        <nav class="level is-mobile">
            <div class="level-item has-text-centered is-marginless">
                <div>
                    <p class="heading">
                        文章
                    </p>
                    <p class="title has-text-weight-normal">
                        70
                    </p>
                </div>
            </div>
            <div class="level-item has-text-centered is-marginless">
                <div>
                    <p class="heading">
                        分类
                    </p>
                    <p class="title has-text-weight-normal">
                        10
                    </p>
                </div>
            </div>
            <div class="level-item has-text-centered is-marginless">
                <div>
                    <p class="heading">
                        标签
                    </p>
                    <p class="title has-text-weight-normal">
                        3
                    </p>
                </div>
            </div>
        </nav>
        <div class="level">
            <a class="level-item button is-link is-rounded" href="https://github.com/koliy/" target="_blank">
                关注我</a>
        </div>
        
        
        <div class="level is-mobile">
            
            <a class="level-item button is-white is-marginless" target="_blank"
                title="Github" href="https://github.com/koliy/">
                
                <i class="fab fa-github"></i>
                
            </a>
            
        </div>
        
    </div>
</div>
    
        


    
        
<div class="card widget">
    <div class="card-content">
        <div class="menu">
            <h3 class="menu-label">
                分类
            </h3>
            <ul class="menu-list">
            <li>
        <a class="level is-marginless" href="/mystudy/categories/3D数学基础-图形与游戏开发/">
            <span class="level-start">
                <span class="level-item">3D数学基础:图形与游戏开发</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">3</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/mystudy/categories/AI/">
            <span class="level-start">
                <span class="level-item">AI</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">12</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/mystudy/categories/MySql/">
            <span class="level-start">
                <span class="level-item">MySql</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/mystudy/categories/SurfaceShader/">
            <span class="level-start">
                <span class="level-item">SurfaceShader</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/mystudy/categories/Unity/">
            <span class="level-start">
                <span class="level-item">Unity</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/mystudy/categories/git/">
            <span class="level-start">
                <span class="level-item">git</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">3</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/mystudy/categories/shade学习/">
            <span class="level-start">
                <span class="level-item">shade学习</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">33</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/mystudy/categories/xlua/">
            <span class="level-start">
                <span class="level-item">xlua</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/mystudy/categories/网络同步/">
            <span class="level-start">
                <span class="level-item">网络同步</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">10</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/mystudy/categories/设计模式/">
            <span class="level-start">
                <span class="level-item">设计模式</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">5</span>
            </span>
        </a></li>
            </ul>
        </div>
    </div>
</div>
    
        <div class="card widget">
    <div class="card-content">
        <div class="menu">
        <h3 class="menu-label">
            归档
        </h3>
        <ul class="menu-list">
        
        <li>
            <a class="level is-marginless" href="/mystudy/archives/2020/09/">
                <span class="level-start">
                    <span class="level-item">九月 2020</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">1</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/mystudy/archives/2020/07/">
                <span class="level-start">
                    <span class="level-item">七月 2020</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">23</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/mystudy/archives/2020/06/">
                <span class="level-start">
                    <span class="level-item">六月 2020</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">17</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/mystudy/archives/2020/03/">
                <span class="level-start">
                    <span class="level-item">三月 2020</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">2</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/mystudy/archives/2020/02/">
                <span class="level-start">
                    <span class="level-item">二月 2020</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">4</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/mystudy/archives/2019/09/">
                <span class="level-start">
                    <span class="level-item">九月 2019</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">5</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/mystudy/archives/2019/08/">
                <span class="level-start">
                    <span class="level-item">八月 2019</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">18</span>
                </span>
            </a>
        </li>
        
        </ul>
        </div>
    </div>
</div>
    
        
<div class="card widget">
    <div class="card-content">
        <h3 class="menu-label">
            标签云
        </h3>
        <a href="/mystudy/tags/game/" style="font-size: 15px;">game</a> <a href="/mystudy/tags/git/" style="font-size: 10px;">git</a> <a href="/mystudy/tags/学习/" style="font-size: 20px;">学习</a>
    </div>
</div>

    
    
        <div class="column-right-shadow is-hidden-widescreen is-sticky">
        
            
<div class="card widget" id="toc">
    <div class="card-content">
        <div class="menu">
            <h3 class="menu-label">
                目录
            </h3>
            <ul class="menu-list"><li>
        <a class="is-flex" href="#纹理凹凸映射">
        <span class="has-mr-6">1</span>
        <span>纹理凹凸映射</span>
        </a><ul class="menu-list"><li>
        <a class="is-flex" href="#高度纹理">
        <span class="has-mr-6">1.1</span>
        <span>高度纹理</span>
        </a></li><li>
        <a class="is-flex" href="#法线纹理">
        <span class="has-mr-6">1.2</span>
        <span>法线纹理</span>
        </a><ul class="menu-list"><li>
        <a class="is-flex" href="#1-在切线空间下计算">
        <span class="has-mr-6">1.2.1</span>
        <span>1. 在切线空间下计算</span>
        </a></li><li>
        <a class="is-flex" href="#2-在世界空间下计算">
        <span class="has-mr-6">1.2.2</span>
        <span>2. 在世界空间下计算</span>
        </a></li></ul></li></ul></li></ul>
        </div>
    </div>
</div>

        
        </div>
    
</div>

                




<div class="column is-4-tablet is-4-desktop is-3-widescreen is-hidden-touch is-hidden-desktop-only has-order-3 column-right is-sticky">
    
        
<div class="card widget" id="toc">
    <div class="card-content">
        <div class="menu">
            <h3 class="menu-label">
                目录
            </h3>
            <ul class="menu-list"><li>
        <a class="is-flex" href="#纹理凹凸映射">
        <span class="has-mr-6">1</span>
        <span>纹理凹凸映射</span>
        </a><ul class="menu-list"><li>
        <a class="is-flex" href="#高度纹理">
        <span class="has-mr-6">1.1</span>
        <span>高度纹理</span>
        </a></li><li>
        <a class="is-flex" href="#法线纹理">
        <span class="has-mr-6">1.2</span>
        <span>法线纹理</span>
        </a><ul class="menu-list"><li>
        <a class="is-flex" href="#1-在切线空间下计算">
        <span class="has-mr-6">1.2.1</span>
        <span>1. 在切线空间下计算</span>
        </a></li><li>
        <a class="is-flex" href="#2-在世界空间下计算">
        <span class="has-mr-6">1.2.2</span>
        <span>2. 在世界空间下计算</span>
        </a></li></ul></li></ul></li></ul>
        </div>
    </div>
</div>

    
    
</div>

            </div>
        </div>
    </section>
    <footer class="footer">
    <div class="container1">
        <div class="level1">
            <div class="level-start1 has-text-centered-mobile1">
                <a class="footer-logo is-block has-mb-6" href="/mystudy/">
                

                
                </a>
                <p class="is-size-7">
                &copy; 2020 Koliy&nbsp;
                Powered by <a href="https://github.com/koliy/koliy.github.io" target="_blank">Hexo</a> & <a
                        href="https://github.com/ppoffice/hexo-theme-icarus" target="_blank">Icarus</a>
                
                <br>
                <span id="busuanzi_container_site_uv">
                共<span id="busuanzi_value_site_uv">0</span>个访客
                </span>
                
                </p>
            </div>
            <div class="level-end1">
            
            </div>
        </div>
    </div>
</footer>
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.3.1/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/moment@2.22.2/min/moment-with-locales.min.js"></script>
<script>moment.locale("zh-CN");</script>

<script>
var IcarusThemeSettings = {
    article: {
        highlight: {
            clipboard: true,
            fold: 'unfolded'
        }
    }
};
</script>


    <script src="https://cdn.jsdelivr.net/npm/clipboard@2.0.4/dist/clipboard.min.js" defer></script>



    
    
    
    <script src="/mystudy/js/animation.js"></script>
    

    
    
    
    <script src="https://cdn.jsdelivr.net/npm/lightgallery@1.6.8/dist/js/lightgallery.min.js" defer></script>
    <script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js" defer></script>
    <script src="/mystudy/js/gallery.js" defer></script>
    

    
    

<div id="outdated">
    <h6>Your browser is out-of-date!</h6>
    <p>Update your browser to view this website correctly. <a id="btnUpdateBrowser" href="http://outdatedbrowser.com/">Update
            my browser now </a></p>
    <p class="last"><a href="#" id="btnCloseUpdateBrowser" title="Close">&times;</a></p>
</div>
<script src="https://cdn.jsdelivr.net/npm/outdatedbrowser@1.1.5/outdatedbrowser/outdatedbrowser.min.js" defer></script>
<script>
    document.addEventListener("DOMContentLoaded", function () {
        outdatedBrowser({
            bgColor: '#f25648',
            color: '#ffffff',
            lowerThan: 'flex'
        });
    });
</script>


    
    
<script src="https://cdn.jsdelivr.net/npm/mathjax@2.7.5/unpacked/MathJax.js?config=TeX-MML-AM_CHTML" defer></script>
<script>
document.addEventListener('DOMContentLoaded', function () {
    MathJax.Hub.Config({
        'HTML-CSS': {
            matchFontHeight: false
        },
        SVG: {
            matchFontHeight: false
        },
        CommonHTML: {
            matchFontHeight: false
        },
        tex2jax: {
            inlineMath: [
                ['$','$'],
                ['\\(','\\)']
            ]
        }
    });
});
</script>

    
    

<a id="back-to-top" title="回到顶端" href="javascript:;">
    <i class="fas fa-chevron-up"></i>
</a>
<script src="/mystudy/js/back-to-top.js" defer></script>


    
    

    
    
    
    

    
    
    
    
    

    


<script src="/mystudy/js/main.js" defer></script>

    
    <div class="searchbox ins-search">
    <div class="searchbox-container ins-search-container">
        <div class="searchbox-input-wrapper">
            <input type="text" class="searchbox-input ins-search-input" placeholder="想要查找什么..." />
            <span class="searchbox-close ins-close ins-selectable"><i class="fa fa-times-circle"></i></span>
        </div>
        <div class="searchbox-result-wrapper ins-section-wrapper">
            <div class="ins-section-container"></div>
        </div>
    </div>
</div>
<script>
    (function (window) {
        var INSIGHT_CONFIG = {
            TRANSLATION: {
                POSTS: '文章',
                PAGES: '页面',
                CATEGORIES: '分类',
                TAGS: '标签',
                UNTITLED: '(无标题)',
            },
            CONTENT_URL: '/mystudy/content.json',
        };
        window.INSIGHT_CONFIG = INSIGHT_CONFIG;
    })(window);
</script>
<script src="/mystudy/js/insight.js" defer></script>
<link rel="stylesheet" href="/mystudy/css/search.css">
<link rel="stylesheet" href="/mystudy/css/insight.css"><!-- hexo-inject:begin --><!-- Begin: Injected MathJax -->
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({"tex2jax":{"inlineMath":[["$","$"],["\\(","\\)"]],"skipTags":["script","noscript","style","textarea","pre","code"],"processEscapes":true},"TeX":{"equationNumbers":{"autoNumber":"AMS"}}});
</script>

<script type="text/x-mathjax-config">
  MathJax.Hub.Queue(function() {
    var all = MathJax.Hub.getAllJax(), i;
    for(i=0; i < all.length; i += 1) {
      all[i].SourceElement().parentNode.className += ' has-jax';
    }
  });
</script>

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js">
</script>
<!-- End: Injected MathJax -->
<!-- hexo-inject:end -->
    
</body>
</html>